package com.hbx.controller;

import com.hbx.constant.ResponseStateConstant;
import com.hbx.constant.RspEnum;
import com.hbx.exception.RspException;
import com.hbx.service.BaseService;
import com.hbx.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/*
 * @Author:hbx
 * 
 * */
@RestController
@RequestMapping("/base")
@Api(tags = "公共处理器")
public class BaseController {

    @Autowired
    private BaseService baseService;

    /**
     * 发送验证码
     * @param email
     * @return
     */
    @PostMapping("/sendCaptcha")
    @ApiOperation("发送验证码接口")
    public Result<String> getCaptcha(@RequestParam("email") @ApiParam(value = "邮箱",required = true) String email){
        if (!StringUtils.hasLength(email)){
            throw new RspException(ResponseStateConstant.PARAMETER_ERROR, "邮箱为必传项");
        }
        return baseService.sendCaptcha(email);
    }

    /**
     * 图片上传接口
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/img/upload")
    @ApiOperation("图片上传接口")
    public Result<String> upload(@RequestParam("file") @ApiParam(value = "文件",required = true) MultipartFile file) {
        if (ObjectUtils.isEmpty(file)){
            throw new RspException(RspEnum.PUBLIC_PARAMETER_ERROR);
        }
        return baseService.uploadFile(file);
    }

    /**
     * 批量上传文件
     * @param files
     * @return
     */
    @PostMapping("/img/uploadBatch")
    @ApiOperation("批量图片上传接口")
    public Result<String> uploadBatch(@RequestParam("files")
                                          @ApiParam(value = "文件",required = true) MultipartFile[] files) {
        Result r = new Result();
        if (ObjectUtils.isEmpty(files)){
            throw new RspException(RspEnum.PUBLIC_PARAMETER_ERROR);
        }
        baseService.uploadFileBatchs(files, r);
        return r;
    }
}
